#include "login.h"
#include "ui_login.h"
#include "regis.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>

extern"C" {
#include"part1.h"
}

Login::Login(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Login)
{
    ui->setupUi(this);
    connect(ui->loginbutton, &QPushButton::clicked, this, [=](){
        const char* file_name="users.txt";

        //读取并转换QString为char*,便于与原始c程序契合。
        QString a =ui->nameline->text();
        QString b =ui->passwordline->text();
        QByteArray a_utf8 = a.toUtf8();
        QByteArray b_utf8 = b.toUtf8();
        char* a1 = a_utf8.data();
        char* b1 = b_utf8.data();

        Users* current_user=userLogin(a1,b1,file_name);
        if(current_user!=NULL){
            emit loginSuccess(current_user->type, a);
            free(current_user);
        }else{
            QMessageBox::warning(this, "Error", "账号或密码错误");
            return;
        }
    });

    connect(ui->regisButton, &QPushButton::clicked, this, [=](){
        regis* m_regis = new regis(this);
        this->hide();
        m_regis->exec();
        this->show();
    });
}

Login::~Login()
{
    delete ui;
}

